const { getBookData, changeField, changeBookStatus } = require('../services/AllBook'),
      { getRecomBookData, changeRecomBookStatus } = require('../services/RecomBook'),
      { getSliderData, changeSliderStatus } = require('../services/Slider'),
      { getAuthorData, changeAuthorStatus, selectStarAuthor } = require('../services/Author'),
      { getPosterBookData, changePosterBookStatus } = require('../services/PosterBook'),
      { API } = require('../config/error_config'),
      { returnInfo } = require('../libs/utils');

class Index {
    //项目初期为了设置session用的，现在貌似是没用了，（不确定）
    async index (ctx, next) {
        const sess = ctx.session;//因为我们koa框架中有了相关配置，我们直接这样就能访问到session

        if (!sess.uid) {
            sess.uid = 1;
            sess.username = 'gc_project';
            sess.nickname = 'gc_pro';
            sess.gender = 'male';
        }

        ctx.body = {
            session: sess
        }
        // await ctx.render('index');
    }

    //获取所有书本信息
    async getBookData (ctx, next) {
        const booksData = await getBookData();

        if (booksData) {
            ctx.body = returnInfo(API.RETURN_SUCCESS, {
                booksData
            })
        }
    }

    //改变书本分类
    async changeBookField (ctx, next) {
        const { bid, field } = ctx.request.body;

        // result是操作数据库数据后，数据受影响的行数
        const result = await changeField(bid, field);

        if (!result) {
            ctx.body = returnInfo(API.CHANGE_FIELD_FAILED);
            return;
        } 

        ctx.body = returnInfo(API.CHANGE_FIELD_SUCCESS);
    }
   
    //获取推荐课程数据
    async getRecomBook (ctx, next) {
        const data = await getRecomBookData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //改变所有产品的上下架状态
    async changeDataStatus (ctx, next) {
        const { id, status, field } = ctx.request.body;
        let result = null;

        switch (field) {
            case 'BOOK':
                result = await changeBookStatus(id, status);
                break;
            case 'RECOM_BOOK':
                result = await changeRecomBookStatus(id, status);
                break;
            case 'SLIDER': 
                result = await changeSliderStatus(id, status);
                break;
            case 'AUTHOR': 
                result = await changeAuthorStatus(id, status);
                break;
            case 'POSTER': //海报广告图 
                result = await changePosterBookStatus(id, status);
                break;
            default:
                ctx.body = returnInfo(API.FIELD_ERROR);
                return;
                break;
        }

        //这个result得到的是数据库数据受影响的行数
        //如果result是0，那就表示没有修改成功
        if (!result) {
            ctx.body = returnInfo(API.CHANGE_STATUS_SUCCESS);
            return;
        }

        ctx.body = returnInfo(API.CHANGE_STATUS_SUCCESS);
    }

    //获取所有海报广告图
    async getPosterBook (ctx, next) {
        const data = await getPosterBookData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //获取轮播图数据
    async getSliders (ctx, next) {
        const data = await getSliderData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //获取作者信息
    async getAuthors (ctx, next) {
        const data = await getAuthorData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //选择是否为明星作者
    async changeAuthorStar (ctx, next) {
        const { id, isStar } = ctx.request.body;

        const result = await selectStarAuthor(id, isStar);

        if (!result) {
            ctx.body = returnInfo(API.SELECT_STAR_TEACHER_FAILED)
            return;
        } 

        ctx.body = returnInfo(API.SELECT_STAR_TEACHER_SUCCESS)
    }
}

module.exports = new Index();